import React from 'react';
import { Form, Row, Col } from 'antd';
import { AxButton } from '@/components/AxBase';
import IconFont from '@/assets/iconfont';

/** 两列布局Modal（24删格布局）
 * @param form 表单
 * @param handleSearch 查询处理函数
 * @param handleResetSearch 重置查询处理函数
 * */
export function AxAdvancedSearchDrawer(props: any) {
    const { form, visible, handleSearch, handleResetSearch, handleVisible, children, ...rest } = props;

    /** 搜索 */
    const handleSubmit = (e: any) => {
        form.validateFields((err: any, fieldsValue: any) => {
            if (err) return;
            if (handleSearch) {
                for (const p in fieldsValue) {
                    const v = fieldsValue[p];
                    /** 去掉空格 */
                    if (v && v.trim) {
                        fieldsValue[p] = v.trim();
                    }
                    /** 处理提交的时间格式 */
                    if (p.toLowerCase().indexOf('time') > -1 || p.toLowerCase().indexOf('created') > -1 || p.toLowerCase().indexOf('date') > -1) {
                        if (v && v.length === 2 && v[0].format && v[1].format) {
                            fieldsValue[p] = v[0].format(v._f) + '~' + v[1].format(v._f);
                            fieldsValue[p + 'Start'] = v[0].format(v._f);
                            fieldsValue[p + 'End'] = v[1].format(v._f);
                        } else if (v && v.format && v._f) {
                            fieldsValue[p] = v.format(v._f);
                        }
                    }
                }
                handleSearch(e, fieldsValue).then(() => {
                    if (handleVisible) handleVisible();
                });
            }
        });
    };

    /** 重置 */
    const handleReset = () => {
        form.resetFields();
        handleResetSearch('clear');
    };

    return (
        <Form
            form={form}
            layout='vertical'
            hidden={!visible}
            {...rest}
        >
            {children}
            <Row key="operate" type="flex" justify="center">
                <Col>
                    <span>
                        <AxButton
                            type="primary"
                            onClick={handleSubmit}
                            icon='search'
                        >
                            查询
                          </AxButton>
                        <AxButton
                            style={{
                                marginLeft: 8,
                            }}
                            onClick={handleReset}
                        >
                            <IconFont type='iconreset' />重置
                    </AxButton>
                    </span>
                </Col>
            </Row>

        </Form>
    );
}

export default AxAdvancedSearchDrawer;